#include <unistd.h>
#include <sys/io.h>

/* The clock frequency of the i8253/i8254 PIT */
#define PIT_TICK_RATE 1193182ul

void beep(unsigned int freq, unsigned int delay)
{
	unsigned int count = PIT_TICK_RATE / freq;
	iopl(3);

	outb_p(0xB6, 0x43);
	outb_p(count & 0xFF, 0x42);
	outb((count >> 8) & 0xFF, 0x42);
	outb_p(inb_p(0x61) | 3, 0x61);
	usleep(1000*delay);
	outb_p(inb_p(0x61) & 0xfc, 0x61);

	iopl(0);
}
void play(unsigned int* freq, unsigned int* time)
{
	int i;
	for(i=0;freq[i]!=0;i++)
	{
		beep(freq[i], time[i]);
	}
}
int main(int argc,char * argv[])
{
	/*Happy Birthday chinese version*/
	unsigned frequency[]=  
	{  
		392,392,440,392,523,494,  
		392,392,440,392,587,523,  
		392,392,784,659,523,494,440,  
		698,698,659,523,587,523  
	};  
	unsigned delay[]=  
	{  
		375,125,500,500,500,1000,  
		375,125,500,500,500,1000,  
		375,125,500,500,500,500,1000,  
		375,125,500,500,500,1000,  
	};
	play(frequency, delay);

	return 0;
}
